package com.yan.springbootstudy.config;

import java.io.IOException;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import com.yan.springbootstudy.common.datasource.MutiDataSource;

/**
 * 
 * 
 * @Title: MyBatisConfig.java
 * @Package com.yan.springbootstudy.config
 * @Description: mybatis配置，主要是扫描所有xml文件
 * @author yan
 * @date 2017年8月31日
 * @version V1.0
 */
@Configuration
@Order(2)
public class MyBatisConfig {
	
	@Autowired
	MutiDataSource datasource;
	
    @Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean() throws IOException{
    	SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
    	sqlSessionFactory.setDataSource(datasource);
    	ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    	Resource[] mapperLocations = resolver.getResources("classpath*:com/yan/**/*Mapper.xml");
		sqlSessionFactory.setMapperLocations(mapperLocations);
		return sqlSessionFactory;
    }
}
